AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

util.PrecacheSound( "Buttons.snd17" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Energy_Increment = 4
local BeepCount = 3
local running = 0

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.damaged = 0
	self.grav = 1
	LS_RegisterEnt(self.Entity)
	
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Habitat", "Pressure", "Temperature", "Gravity", "On" }) end

	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(20)

	end
end

function ENT:Setup()
	

	self:ShowOutput(0, 0, 0)
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 0) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Habitat", 0) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Pressure", 0) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Temperature", 0) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Gravity", 0) end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
				self.Entity:EmitSound( "Buttons.snd17" )
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				self.Entity:EmitSound( "Buttons.snd17" )
			end
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end

function ENT:Output()
	return 1
end

function ENT:Use()
end

function ENT:Sense()
	energy = RD_GetResourceAmount(self, "energy")
	if (energy <= 0) then
		self.Entity:EmitSound( "common/warning.wav" )
		if (running == 1) then
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Habitat", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Pressure", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Temperature", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Gravity", 0) end
		end
		running = 0
		return
	else
		running = 1
		if (BeepCount > 0) then
			BeepCount = BeepCount - 1
		else
			self.Entity:EmitSound( "Buttons.snd17" )
			BeepCount = 3
		end
	end
	self.gravity = self.grav
	if (self.damaged == 1) then
		local test = math.random(1, 10)
		if (test <= 2) then
			self.environment.habitat = math.random(0, 1)
		elseif (test <= 3) then
			self.environment.atmosphere = self.environment.atmosphere + math.random(-100, 100)
		elseif (test <= 4) then
			self.environment.temperature = self.environment.temperature + math.random((1 - self.environment.temperature), self.environment.temperature)
		elseif (test <= 5) then
			self.gravity = math.random(0, 1)
		end
		
		local test = math.random(1, 10)
		if (test <= 2) then
			self.environment.habitat = math.random(0, 1)
		elseif (test <= 3) then
			self.environment.atmosphere = self.environment.atmosphere + math.random(-100, 100)
		elseif (test <= 4) then
			self.environment.temperature = self.environment.temperature + math.random((1 - self.environment.temperature), self.environment.temperature)
		elseif (test <= 5) then
			self.gravity = math.random(0, 1)
		end
	end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 1) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Habitat", self.environment.habitat) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Pressure", self.environment.atmosphere) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Temperature", self.environment.temperature) end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Gravity", self.gravity) end
	RD_ConsumeResource(self, "energy", Energy_Increment)

end


function ENT:ShowOutput( )
	if (self.Active == 1) then
		if (running == 1) then
			self:SetOverlayText( "Atmospheric Probe (ON)\nHabitable: " .. self.environment.habitat .. "\nPressure: " .. self.environment.atmosphere .. " Atmospheres\nTemperature: " .. self.environment.temperature .. " Deg. K\nGravity: " .. self.grav )
		else
			self:SetOverlayText( "Atmospheric Probe (ON)\nEnergy: 0" )
		end
	else
		self:SetOverlayText( "Atmospheric Probe (OFF)" )
	end
end

function ENT:Think()
	self:ShowOutput()
	if ( self.timer > CurTime() ) then return end
	if (self.Active == 1) then
		self:Sense()
	end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Entity:EmitSound( "Buttons.snd17" )
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 1) end
			self.Active = 1
		else
			self.Entity:EmitSound( "Buttons.snd17" )
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Habitat", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Pressure", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Temperature", 0) end
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Gravity", 0) end
			self.Active = 0
		end
	end
end


function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
